#include "StandardFieldBuilder.h"
#include "Field.h"
#include "Ball.h"

#include <QSharedPointer>
#include <QGraphicsItem>

QSharedPointer<Field> StandardFieldBuilder::createField()
{
	Field *field = new Field(QSize(7, 7));

	addUpperBlock(field);
	addLeftBlock(field);
	addRightBlock(field);
	addLowerBlock(field);
	addCentralBlock(field);
	clearCell(field, QPoint(3, 3));

	return QSharedPointer<Field>(field);
}

void StandardFieldBuilder::addUpperBlock(Field *field)
{
	addBlock(field, 0, 2, 2, 5);
}

void StandardFieldBuilder::addLeftBlock(Field *field)
{
	addBlock(field, 2, 5, 0, 2);
}

void StandardFieldBuilder::addRightBlock(Field *field)
{
	addBlock(field, 2, 5, 5, 7);
}

void StandardFieldBuilder::addLowerBlock(Field *field)
{
	addBlock(field, 5, 7, 2, 5);
}

void StandardFieldBuilder::addCentralBlock(Field *field)
{
	addBlock(field, 2, 5, 2, 5);
}

void StandardFieldBuilder::addBlock(Field *field, int up, int down, int left, int right)
{
	for (int i = up; i < down; i++) {
		for (int j = left; j < right; j++) {
			addCell(field, QPoint(i, j));
			if (i != 3 || j != 3) {
				addBall(field, QPoint(i, j), QSharedPointer<QGraphicsObject>(new Ball()));
			}
		}
	}
}
